package com.wn.class30.cotroller.shoppingmall;

import com.wn.class30.entity.Business;
import com.wn.class30.entity.Product;
import com.wn.class30.entity.ProductImgDetail;
import com.wn.class30.entity.ProductImgHead;
import com.wn.class30.service.ShoppingMallHomeService;
import com.wn.class30.tools.Result;
import com.wn.class30.tools.UseridUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

//商城首页的控制层
@RestController
@RequestMapping("/ShoppingMallHome")
@Slf4j
@Api("商城主页控制类")
public class ShoppingMallHomeController {
    @Resource
    private UseridUtils useridUtils;
    @Resource
    private ShoppingMallHomeService shoppingMallHomeService;

    //查询单个商品数据      http://localhost:8080/weixin/shoppingmall/index.html
    @GetMapping("/getProduct")
    @ApiOperation(value = "商城主页猜你喜欢查询商品方法")
    public Result getProduct(HttpServletRequest req) throws Exception {
//        Integer userid = useridUtils.queryUseridByOptenid(req);
        Integer userid = 233;
        System.out.println("userid"+userid);
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            List<Product> product = shoppingMallHomeService.getProduct();
            return  new Result("200","success",null,product);
        }
    }

    //查询新品的商品数据      http://localhost:8080/weixin/shoppingmall/index.html
    @GetMapping("/getProductWithNew")
    @ApiOperation(value = "商城主页查询新品方法")
    public Result getProductWithNew(@Valid @NotNull Integer limit, HttpServletRequest req) throws Exception {
//        Integer userid = useridUtils.queryUseridByOptenid(req);
        Integer userid = 233;
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            //查询所有
            Long total = shoppingMallHomeService.getTotal();
            List<Product> product = shoppingMallHomeService.getProductWithNew(total - 9L,limit);
            return  new Result("200","success",null,product);
        }
    }
    //查询爆款的商品数据      http://localhost:8080/weixin/shoppingmall/index.html
    @GetMapping("/getProductWithHot")
    @ApiOperation(value = "商城主页查询爆款方法")
    public Result getProductWithHot(HttpServletRequest req) throws Exception {
//        Integer userid = useridUtils.queryUseridByOptenid(req);
        Integer userid = 233;
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            List<Product> product = shoppingMallHomeService.getProductWithHot();
            return  new Result("200","success",null,product);
        }
    }
    //查询商品轮播图
    @GetMapping("/getProductImgHeadByProductId")
    @ApiOperation(value = "查询商品轮播图")
    public Result getProductImgHeadByProductId(HttpServletRequest req,@Valid @NotNull Integer productId) throws Exception {
//        Integer userid = useridUtils.queryUseridByOptenid(req);
        Integer userid = 233;
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            List<ProductImgHead> productImgHead = shoppingMallHomeService.getProductImgHeadByProductId(productId);
            return  new Result("200","success",null,productImgHead);
        }
    }
    //查询商品详情
    @GetMapping("/getProductByProductId")
    @ApiOperation(value = "查询商品详情")
    public Result getProductByProductId(HttpServletRequest req,@Valid @NotNull Integer productId) throws Exception {
//        Integer userid = useridUtils.queryUseridByOptenid(req);
        Integer userid = 233;
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            Product product = shoppingMallHomeService.getProductByProductId(productId);
            return  new Result("200","success",product,null);
        }
    }
    //查询商品详情
    @GetMapping("/getProductImgDetailByProductId")
    @ApiOperation(value = "查询商品详情")
    public Result getProductImgDetailByProductId(HttpServletRequest req,@Valid @NotNull  Integer productId) throws Exception {
//        Integer userid = useridUtils.queryUseridByOptenid(req);
        Integer userid = 233;
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            List<ProductImgDetail> productImgDetail = shoppingMallHomeService.getProductImgDetailByProductId(productId);
            return  new Result("200","success",null,productImgDetail);
        }
    }

    /**
     * 查询商品商家
     * @param req
     * @param productId
     * @return
     * @throws Exception
     */
    @GetMapping("/getProductBusinessByProductId")
    @ApiOperation(value = "根据商品id查询商家详情")
    public Result getProductBusinessByProductId(HttpServletRequest req,@Valid @NotNull  Integer productId) throws Exception {
//        Integer userid = useridUtils.queryUseridByOptenid(req);
        Integer userid = 233;
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            List<Business> business = shoppingMallHomeService.getProductBusinessByProductId(productId);
            return  new Result("200","success",null,business);
        }
    }
    @GetMapping("/selectBusinessByBusinessId")
    @ApiOperation(value = "根据商家id查询商家详情")
    public Result selectBusinessByBusinessId(HttpServletRequest req,@Valid @NotNull  Integer businessId) throws Exception {
            Business business = shoppingMallHomeService.selectBusinessByBusinessId(businessId);
            return  new Result("200","success",business,null);
        }

    @GetMapping("/selectProductsByBusinessId")
    @ApiOperation(value = "根据商家id查询所有商品详情")
    public Result selectProductsByBusinessId(HttpServletRequest req,@Valid @NotNull  Integer businessId) throws Exception {
        List<Product> products = shoppingMallHomeService.selectProductsByBusinessId(businessId);
        return  new Result("200","success",null,products);
    }
    }

